$(function() {
    let form = layui.form
    let laypage = layui.laypage;
    let layer = layui.layer
        // 定义一个查询q对象
    let q = {
        pagenum: 1,
        pagesize: 2,
        cate_id: '',
        state: ''
    }
    initTable()
    initCate()
        // 定义美化时间的过滤器
    template.defaults.imports.dataFormat = function(date) {
        const dt = new Date(date)

        var y = dt.getFullYear()
        var m = padZero(dt.getMonth() + 1)
        var d = padZero(dt.getDate())

        var hh = padZero(dt.getHours())
        var mm = padZero(dt.getMinutes())
        var ss = padZero(dt.getSeconds())

        return y + '-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss
    }

    // 定义补零的函数
    function padZero(n) {
        return n > 9 ? n : '0' + n
    }

    function initTable() {
        $.ajax({
            method: 'GET',
            url: '/my/article/list',
            data: q,
            success: function(res) {
                if (res.code !== 0) {
                    return layer.msg('获取文章列表失败！')
                }
                let htmlStr = template('tpl_table', res)
                $('tbody').html(htmlStr)
                renderPage(res.total)
            }
        })
    }


    function initCate() {
        $.ajax({
            method: 'GET',
            url: '/my/cate/list',
            success: function(res) {
                if (res.code !== 0) {
                    return layer.msg('获取分类数据失败！')
                }
                let htmlStr = template('tpl_cate', res)
                    // console.log(htmlStr);
                $('[name=cate_id]').html(htmlStr)
                form.render()
            }
        })
    }
    $('#form_search').on('submit', function(e) {
        e.preventDefault()
        let cate_id = $('[name=cate_id]').val()
        let state = $('[name=state]').val()
        q.cate_id = cate_id
        q.state = state
        initTable()

    })

    function renderPage(total) {
        laypage.render({
            elem: 'tpl_page', //注意，这里是 ID，不用加 # 号
            count: total, //数据总数，从服务端得到
            limit: q.pagesize, //每页限制条数
            curr: q.pagenum, //默认显示哪一页
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [2, 3, 5, 10, 20],
            jump: function(obj, first) {
                // console.log(first);
                q.pagenum = obj.curr
                q.pagesize = obj.limit
                if (!first) {
                    initTable()
                }
            }
        });
    }
    $('tbody').on('click', '.btn_del', function() {
        let len = $('.btn_del').length
        let id = $(this).attr('data-id')
        layer.confirm('确认删除?', { icon: 3, title: '提示' }, function(index) {
            //do something
            $.ajax({
                method: 'delete',
                url: '/my/article/info?id=' + id,
                success: function(res) {
                    if (res.code !== 0) {
                        return layer.msg('删除失败！')
                    }
                    layer.msg('删除成功！')
                    if (len === 1) {
                        q.pagenum = q.pagenum === 1 ? 1 : q.pagenum - 1
                    }
                    layer.close(index);
                    initTable()
                }

            })
        });
    })

})